import express from "express";
import { PrismaClient, Prisma } from "@prisma/client";
import { CreateId, Check, Result, Time, Utils } from "@/utils";

const prisma = new PrismaClient();
const router = express.Router();

/** 添加路由信息 */
router.post("/create", async (req, res) => {
   const params: RouterConfig.Create = req.body;
   if (!Check.Required([params.component, params.meta_label, params.path])) return res.send(Result.Error("参数错误"));

   const { userId } = req;
   const id = await CreateId.One("mp_router");

   let parent;
   if (!!params.parent_id) parent = await prisma.mp_router.findUnique({ where: { id: params.parent_id } });

   await prisma.mp_router.create({
      data: {
         id,
         parent_id: params.parent_id || null,
         created_time: Time.Now(),
         level: parent ? parent.level + 1 : 1,
         updated_by: userId,
         created_by: userId,
         updated_time: Time.Now(),
         component: params.component,
         meta_label: params.meta_label,
         path: params.path,
         meta_icon: params.meta_icon,
         name: params.name
      }
   });
   res.send(Result.OK());
});

/** 删除路由 */
router.post("/delete", async (req, res) => {
   const { id } = req.body as RouterConfig.Delete;
   if (!Check.Required([id])) return res.send(Result.Error("参数错误"));

   await prisma.mp_router.delete({ where: { id } });

   res.send(Result.OK());
});

// 修改路由内容
router.post("/update", async (req, res) => {
   const value: RouterConfig.Update = req.body;
   const { userId } = req;
   if (!Check.Required([value.id, value.component, value.meta_label, value.path])) return res.send(Result.Error("参数错误"));

   const oldValue = await prisma.mp_router.findUnique({ where: { id: value.id } });
   if (!oldValue) return res.send(Result.Error("当前条目不存在"));

   let parent;
   let level = 1;
   if (value.parent_id) parent = await prisma.mp_router.findUnique({ where: { id: value.parent_id } });
   if (parent) level = parent.level + 1;

   await prisma.mp_router.update({
      where: { id: value.id },
      data: {
         component: value.component,
         meta_label: value.meta_label,
         meta_icon: value.meta_icon || oldValue.meta_icon,
         path: value.path,
         level,
         parent_id: value.parent_id || oldValue.parent_id,
         name: value.name || oldValue.name,
         updated_by: userId,
         updated_time: Time.Now()
      }
   });

   res.send(Result.OK());
});

router.get("/search", async (req, res) => {
   const { parent_id }: RouterConfig.Search = req.query as any;

   let list;
   const select: Prisma.mp_routerSelectScalar = {
      meta_label: true,
      meta_icon: true,
      component: true,
      id: true,
      level: true,
      parent_id: true,
      name: true,
      path: true
   };

   if (parent_id) {
      list = await prisma.mp_router.findMany({ where: { parent_id }, select });
   } else {
      list = await prisma.mp_router.findMany({ select });
   }

   const tree = Utils.ListToTree(list);
   res.send(Result.OK(tree));
});

export const routerConfig = router;
